package uia.alumni.profile;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.http.HttpServletRequest;

import static uia.alumni.form.FieldType.*;
import static uia.alumni.profile.Constants.UID;
import static uia.alumni.web.Constants.*;
import static uia.alumni.group.Constants.GID;

import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.data.Group;
import uia.alumni.form.*;
import uia.alumni.web.*;
import uia.alumni.exception.*;
import uia.alumni.group.ViewGroup;

/**
 * Show the Profile of a User. If User is logged in we will show more options.
 * @author simon
 */
public class ViewProfile extends Page {

    public static final String SERVLET_NAME = "profile.view";
    @PersistenceUnit(name = PERSISTENCE_UNIT)
    private EntityManagerFactory emf;

    public ViewProfile() {
        super(Role.member);
    }

    @Override
    public void writeBody(PrintWriter out, HttpServletRequest request) {
        EntityManager em = emf.createEntityManager();

        try {
            User user = getUser(em, request);

            // Does the user exists?
            if (user != null) {
                // Check if current user has some extra roles.
                boolean isAdmin = isAdmin(em, request);
                boolean isOwner = isOwner(em, request, user);
                
                // Vars for convinience
                String suid = Long.toString(user.getUid());
                String name = user.getFullName();

                // User groups
                ArrayList groups = new ArrayList<String>();
                for (Group group : user.getGroups()) {
                    groups.add(tagA(ViewGroup.SERVLET_NAME, group.getName(), GID, Long.toString(group.getId())));
                }

                // Is the logged in user the Owner or Admin
                if (isOwner || isAdmin) {
                    //name = tagA(EditProfile.SERVLET_NAME, name);
                    name = tagA(EditProfile.SERVLET_NAME, name , UID, suid);
                }

                // Print data
                out.println(tagH1(name));
                if(isAdmin){
                    out.println(getAdminInfo());
                    out.println("<ul>");
                    out.println(tagLi(tagA(DeleteProfile.SERVLET_NAME, "DELETE <em>"+user.getFullName()+"</em>", UID, suid)));
                    out.println("</ul>");
                    out.println("</div>");
                }
                out.println("<dl>");
                out.println(tagDD("Name:", name));
                out.println(tagDD("Email:", user.getEmail()));
                out.println(tagDD("Address:", user.getAddress()));
                out.println(tagDD("Phone:", user.getPhone()));
                out.println(tagDD("Description: ", user.getDescription()));
                out.println(tagDD("Groups: ", tagUl(groups)));
                out.println("</dl>");
                //out.print(tagUl(groups));

            } else {
                throw new NotFoundException("The User you are looking for does not exist");
            }
        } catch (Exception e) {
            addMessage(request, e.getMessage());
        } finally {
            //
        }
    }
}
